package com.baomidou.ant.controller;


import com.baomidou.ant.common.Result;
import com.baomidou.ant.service.IMachineInfoService;
import com.baomidou.ant.vo.request.RequestAddMachineInfoVo;
import com.baomidou.ant.vo.request.RequestListMachineInfoVo;
import com.baomidou.ant.vo.response.ResponseListMachineInfoVo;
import com.baomidou.ant.vo.response.ResponseMachineNameVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/machine-info")
public class MachineInfoController {
    @Autowired
    private IMachineInfoService machineInfoService;

    @PostMapping("/listMachine")
    public Result listMachine(@RequestBody RequestListMachineInfoVo vo){
        IPage<ResponseListMachineInfoVo> page=machineInfoService.listMachine(vo);
        return Result.getSuccess().setData(page);
    }

    @GetMapping("/listMachineType/{deptId}")
    public Result listMachineType(@PathVariable String deptId){
        List<ResponseMachineNameVo> list=machineInfoService.listMachineName(Integer.parseInt(deptId));
        return Result.getSuccess().setData(list);
    }

    //新增机型接口
    @PostMapping("/addMachineInfo")
    public Result addMachineInfo(@RequestBody RequestAddMachineInfoVo vo){
        if(ObjectUtils.isNotEmpty(machineInfoService.queryByModel(vo))){
            return Result.getFailure().setMsg("新增失败!</br>该部门已经存在此机型");
        }
        if(machineInfoService.addMachine(vo)){
            return Result.getSuccess().setMsg("新增机型成功");
        }else
            return Result.getFailure().setMsg("新增失败！插入失败");
    }

}
